<?php
class AppController extends Controller {
	var $helpers = array('Html','Form','Javascript','Site');
	var $luser = null;

	function beforeFilter() {
		if($this->luser=$this->Session->read('User')) return;
		if(!$this->luser && $this->action != "auth" && $this->action != "logout") {
			if(isset($_COOKIE["luser"])) {
				if(stripos($this->referer(),'http://')===false) {
					$this->Session->write('redir', "/".$this->params['url']['url']);
					$this->redirect("/users/auth/");
					exit;
				}
			}
		}
	}

	function beforeRender() {
		$this->set("luser",$this->luser);
	}

  function checkSession() {
    if (!$this->Session->check('User'))
    {
		$this->Session->setFlash('You need to log in.','msgerror');
      $this->redirect('/users/login/');
      exit;
    }
  }

	function checkAdmin() {
		if (!$this->Session->check('User') || $this->luser['type']!=2) {
       $this->redirect('/users/login/');
       exit;
		}
	}

	function useModel($models,&$instance) {
		if (!is_array($models)) $models = array($models);
    foreach ($models as $model){
      if (!isset($instance->{$model})){
         if (!class_exists($model)){
            loadModel($model);
         }
         if (class_exists($model)){
            $instance->{$model} = &new $model();
         }
      }
    }
	}

	function pager($count,$pagesize,$par='p') {
    $pages=ceil($count / $pagesize);
    if(isset($this->params['url'][$par])) $page=$this->params['url'][$par]; else $page=1;
    $this->set('pages', $pages); $this->set('page', $page);
		$this->pages=$pages; $this->page=$page;
		return $page;
	}

}
?>